import { createSlice, configureStore } from "@reduxjs/toolkit";

const counterSlice = createSlice({
  name: "userinfo",
  initialState: {
    user: {},
    defaultStoreId: "",
  },
  reducers: {
    setUser: (state, actions) => {
      // mutation 突变
      // return { ...state, ...actions.payload}
      // 突变的写法，内置了immer，允许我们使用突变的写法实现非突变的数据更新
      state.user = actions.payload.user;
      state.defaultStoreId = actions.payload.defaultStoreId;
    },
  },
});

// incremented() ==> {type: 'UOPDATE', payload: {}}
// type === 'userinfo/setUer'
export const { setUser } = counterSlice.actions;

export const selectStoreId = (state) => {
  return state.user.defaultStoreId;
};

export default counterSlice.reducer;
